package org.moflon.boardgamegui.view;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.moflon.boardgamegui.controller.GenericController;

public class GUIField<GenericBoard extends EObject,
					  GenericField extends EObject,
					  GenericFigure extends EObject> extends MouseAdapter {

	private GenericController<GenericBoard, GenericField, GenericFigure> controller;
	private Button button;
	private GenericField field;
	private int size;
	private int imageReduceFactor = 0;
	private Map<Image, Image> m_SizeImagesMap = new HashMap<Image, Image>();
	private Map<Image, Image> m_popUpMenuSizeImagesMap = new HashMap<Image, Image>();

	public GenericField getField() {
		return field;
	}

	public void setField(GenericField field) {
		this.field = field;
	}

	protected GUIField(GenericController<GenericBoard, GenericField, GenericFigure> controller, Composite parent, int fieldSize) {
		this.controller = controller;
		this.size = fieldSize;
		button = new Button(parent, SWT.NONE);

		GridData gd_btnNewButton = new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = fieldSize;
		gd_btnNewButton.heightHint = fieldSize;
		button.setLayoutData(gd_btnNewButton);
		button.addMouseListener(this);
	

		createPopupMenu();
		update("empty");
	}

	@Override
	public void mouseUp(MouseEvent e) {
		if (e.button == 1)
			controller.fieldPressed(this.field);
	}

	protected Menu createPopupMenu() {
		Menu popupMenu = new Menu(button);

		int id = 0;
		for (String figureName : controller.getFigureNames()) {
			MenuItem actItem = new MenuItem(popupMenu, 0, 0);

			actItem.addSelectionListener(new PopmenuAction(controller, this));
			actItem.setText(figureName);

			Image img = SWTResourceManager.getImage("./resources/" + figureName
					+ ".png");
			Image scaled = m_popUpMenuSizeImagesMap.get(img);
			if (scaled == null) {
				scaled = new Image(img.getDevice(), img.getImageData()
						.scaledTo((int) (20), (int) (20)));
				m_popUpMenuSizeImagesMap.put(img, scaled);
			}
			actItem.setImage(scaled);

			// Add index in collection of figures as id for callback
			actItem.setID(id++);
		}

		button.setMenu(popupMenu);
		return popupMenu;
	}

	/**
	 * This Method updates the appearance of a Field by setting a corresponding
	 * Image according to its name. The Images are located in
	 * /BoardGameGUI/resources/ .
	 * 
	 * @param name
	 *            The name of the new image decorating the GUiField (e.g.,
	 *            /BoardGameGUI/resources/'<em><b>name</b></em>'.png
	 */
	public void update(String name) {
		if (name != null) {
			Image img = SWTResourceManager.getImage("./resources/" + name
					+ ".png");
			Image scaled = m_SizeImagesMap.get(img);
			if (scaled == null) {
				scaled = new Image(img.getDevice(), img.getImageData()
						.scaledTo(size - imageReduceFactor,
								size - imageReduceFactor));
				m_SizeImagesMap.put(img, scaled);
			}
			button.setImage(scaled);
		} 
	}
}
